
% convenience function for tracking one high resolution widefield movie.
% Set to work with the wrapper function highResWidefieldWrapper_v1_0.m

function [iosmov,gcampmov,traj] = trackOneWidefieldMovie(iosmov,gcampmov,msk,refim,masterRefIm,vesselSkeleton,sessionRegister,doGcampBloodCorrection,baselineframes,skipIOStracking)

% optional input for doing the correction for gcamp; in the past this cause
% some issues but hopefully this is resolved now?
if nargin < 8 || isempty(doGcampBloodCorrection)
    doGcampBloodCorrection = false;
end
if nargin < 10 || isempty(skipIOStracking)
    skipIOStracking = false;
end
%% Apply window mask:
temp = iosmov( (round(size(iosmov,1)/2)-10):(round(size(iosmov,1)/2)+10),...
    (round(size(iosmov,2)/2)-10):(round(size(iosmov,2)/2)+10),:);
bgval_ios = median(temp(:));
for n = 1:size(iosmov,3)
    iosmov(:,:,n) = iosmov(:,:,n).*msk;
end

iosmov(iosmov==0) = bgval_ios;
refim(~msk) = bgval_ios;

temp = gcampmov( (round(size(gcampmov,1)/2)-10):(round(size(gcampmov,1)/2)+10),...
    (round(size(gcampmov,2)/2)-10):(round(size(gcampmov,2)/2)+10),:);
bgval_gcamp = median(temp(:));

for n = 1:size(gcampmov,3)
    gcampmov(:,:,n) = gcampmov(:,:,n).*msk;
end

gcampmov(gcampmov==0) = bgval_gcamp;


%% run image registration for small motion during the movie
if ~isempty(refim)
    [iosmov,imtrans] = driftCorrectWidefield(iosmov,refim,[],[],"multimodal");
    f = waitbar(0,'percent movie registered');
    for n = 1:size(gcampmov,3)
        gcampmov(:,:,n) = imwarp(gcampmov(:,:,n),imtrans(n),'OutputView',imref2d([size(gcampmov,1),size(gcampmov,2)]));
        waitbar(n/size(gcampmov,3),f);
    end
    close(f)
    % if have some zero pixels due to shifting the image set those to
    % background levels:
    iosmov(iosmov==0) = bgval_ios;
end



%% run image registration to the master image so can have everything registered across sessions:
if nargin > 6 && ~isempty(sessionRegister)
    f = waitbar(0,'registering to parent session');
    for n = 1:size(gcampmov,3)
        gcampmov(:,:,n) = imwarp(gcampmov(:,:,n),sessionRegister.Transformation,'OutputView',imref2d([size(gcampmov,1),size(gcampmov,2)]));
        iosmov(:,:,n) = imwarp(iosmov(:,:,n),sessionRegister.Transformation,'OutputView',imref2d([size(iosmov,1),size(iosmov,2)]));
        waitbar(n/size(gcampmov,3),f);
    end
    close(f)
elseif nargin > 4 && ~isempty(masterRefIm)
    %need to make sure there's enough space for any kind of resizing that
    %may need to happen:
    gcampRegistered = zeros(size(masterRefIm,1),size(masterRefIm,2),size(gcampmov,3));
    iosRegistered = zeros(size(masterRefIm,1),size(masterRefIm,2),size(iosmov,3));
    %[~,sessionRegister] = driftCorrectWidefield(refim,masterRefIm,[],'rigid',"multimodal");         %need to make sure you're doing multimodal registration because intensities will be different between sessions
    [sessionRegister] = registerMultipleWidefieldImages(refim,masterRefIm);
    f = waitbar(0,'registering to parent session');
    for n = 1:size(gcampmov,3)
        %option 1 that we know works is registering everything in the frame
        %of the local movie; this suffers from the fact that images in
        %different sessions are different sizes
        % gcampmov(:,:,n) = imwarp(gcampmov(:,:,n),sessionRegister.Transformation,'OutputView',imref2d([size(gcampmov,1),size(gcampmov,2)]));
        % iosmov(:,:,n) = imwarp(iosmov(:,:,n),sessionRegister.Transformation,'OutputView',imref2d([size(iosmov,1),size(iosmov,2)]));

        %option 2 is to register everything in the size of the master
        %reference image, which should make everything the correct size
        % gcampmov(:,:,n) = imwarp(gcampmov(:,:,n),sessionRegister.Transformation,'OutputView',imref2d([size(masterRefIm,1),size(masterRefIm,2)]));
        % iosmov(:,:,n) = imwarp(iosmov(:,:,n),sessionRegister.Transformation,'OutputView',imref2d([size(masterRefIm,1),size(masterRefIm,2)]));
        gcampRegistered(:,:,n) = imwarp(gcampmov(:,:,n),sessionRegister.Transformation,'OutputView',imref2d([size(masterRefIm,1),size(masterRefIm,2)]));
        iosRegistered(:,:,n) = imwarp(iosmov(:,:,n),sessionRegister.Transformation,'OutputView',imref2d([size(masterRefIm,1),size(masterRefIm,2)]));
        waitbar(n/size(gcampmov,3),f);
    end
    gcampmov = gcampRegistered;clear gcampRegistered
    iosmov = iosRegistered;clear iosRegistered
    close(f)
end
iosmov(iosmov<1) = bgval_ios;

%% add in correction for blood volume changes to the gcamp data:
if doGcampBloodCorrection
    temp = iosmov./repmat(mean(iosmov(:,:,baselineframes),3,'omitnan'),1,1,size(iosmov,3));
    gcampmov = gcampmov./temp;
    clear temp
end

%% once all the image registering is done can run tracking of IOS:
if skipIOStracking
    traj = struct('vesselCent',[],'vesselWidth',[],'vesselTotalInten',[],'surroundInten',[],'maxInten',[]);
else
    traj = trackWidefieldLinescans_v1_0(iosmov,vesselSkeleton);
end
